package com.linx.notice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.linx.common.core.service.OssService;
import com.linx.common.core.utils.MapstructUtils;
import com.linx.common.core.utils.StringUtils;
import com.linx.common.mybatis.core.page.PageQuery;
import com.linx.common.mybatis.core.page.TableDataInfo;
import com.linx.common.satoken.utils.LoginHelper;
import com.linx.notice.domain.InfoRealease;
import com.linx.notice.domain.bo.InfoRealeaseBo;
import com.linx.notice.domain.vo.InfoRealeaseVo;
import com.linx.notice.mapper.InfoRealeaseFileMapper;
import com.linx.notice.mapper.InfoRealeaseMapper;
import com.linx.notice.service.IInfoRealeaseService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * 信息发布Service业务层处理
 *
 * @author 泽智科技
 * @date 2024-10-21
 */
@RequiredArgsConstructor
@Service
public class InfoRealeaseServiceImpl implements IInfoRealeaseService {

    private final InfoRealeaseMapper realeaseMapper;
    private final InfoRealeaseFileMapper realeaseFileMapper;
    private final OssService ossService;

    /**
     * 查询信息发布
     *
     * @param infoReleaseId 主键
     * @return 信息发布
     */
    @Override
    public InfoRealeaseVo queryById(Long infoReleaseId) {
        InfoRealeaseVo vo = realeaseMapper.selectVoById(infoReleaseId);
        //List<Long> fileIds = realeaseFileMapper.selectByReleaseId(infoReleaseId);
        //vo.setFileUrls(fileIds);
        return vo;
    }

    /**
     * 分页查询信息发布列表
     *
     * @param query        查询条件
     * @param pageQuery 分页参数
     * @return 信息发布分页列表
     */
    @Override
    public TableDataInfo<InfoRealeaseVo> queryPageList(InfoRealeaseBo query, PageQuery pageQuery) {
        Page<InfoRealeaseVo> result = realeaseMapper.selectVoPageNew(pageQuery.build(), query);

        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的信息发布列表
     *
     * @param bo 查询条件
     * @return 信息发布列表
     */
    @Override
    public List<InfoRealeaseVo> queryList(InfoRealeaseBo bo) {
        LambdaQueryWrapper<InfoRealease> lqw = buildQueryWrapper(bo);
        return realeaseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<InfoRealease> buildQueryWrapper(InfoRealeaseBo bo) {
        LambdaQueryWrapper<InfoRealease> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getInfoTitle()), InfoRealease::getInfoTitle, bo.getInfoTitle());
        lqw.eq(bo.getInfoTypeId() != null, InfoRealease::getInfoTypeId, bo.getInfoTypeId());
        lqw.eq(bo.getInfoScopeId() != null, InfoRealease::getInfoScopeId, bo.getInfoScopeId());
        return lqw;
    }

    /**
     * 新增信息发布
     *
     * @param bo 信息发布
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(InfoRealeaseBo bo) {
        InfoRealease add = MapstructUtils.convert(bo, InfoRealease.class);
        validEntityBeforeSave(add);


        if ("1".equals(bo.getRealeaseTimeType())) {
            add.setRealeaseTime(new Date());
        }
        if ("2".equals(bo.getRealeaseType())) {
            if ("1".equals(bo.getRealeaseTimeType())) {
                add.setRealeaseStatus("2");

            } else {
                add.setRealeaseStatus("1");
            }
        }

        boolean flag = realeaseMapper.insert(add) > 0;
        if (flag) {
            bo.setInfoReleaseId(add.getInfoReleaseId());
            ossService.updateBiz(bo.getInfoReleaseId(), InfoRealease.class, "info_realease_header_pic",
                Collections.singletonList(bo.getInfoHeaderPicId()));
            ossService.updateBiz(bo.getInfoReleaseId(), InfoRealease.class, "info_realease_banner_id",
                Collections.singletonList(bo.getBannerId()));
            //if(bo.getFileUrls().size()>0){
            //    List<InfoRealeaseFile> realeaseFiles = bo.getFileUrls().stream().map(e->{
            //        InfoRealeaseFile realeaseFile = new InfoRealeaseFile();
            //        realeaseFile.setFileUrl(e);
            //        realeaseFile.setInfoRealeaseId(add.getInfoReleaseId());
            //        return  realeaseFile;
            //    }).collect(Collectors.toList());
            //    realeaseFileMapper.insertBatch(realeaseFiles);
            //}
        }
        return flag;
    }

    /**
     * 修改信息发布
     *
     * @param bo 信息发布
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(InfoRealeaseBo bo) {
        InfoRealease update = MapstructUtils.convert(bo, InfoRealease.class);
        if ("1".equals(bo.getRealeaseTimeType())) {
            update.setRealeaseTime(new Date());
        }
        if ("2".equals(bo.getRealeaseType())) {
            if ("1".equals(bo.getRealeaseTimeType())) {
                update.setRealeaseStatus("2");

            } else {
                update.setRealeaseStatus("1");
            }
        }
        validEntityBeforeSave(update);
        boolean flag = realeaseMapper.updateById(update) > 0;
        //if(bo.getFileUrls().size()>0){
        //    realeaseFileMapper.deleteByRealeaseId(bo.getInfoReleaseId());
        //    List<InfoRealeaseFile> realeaseFiles = bo.getFileUrls().stream().map(e->{
        //        InfoRealeaseFile realeaseFile = new InfoRealeaseFile();
        //        realeaseFile.setFileUrl(e);
        //        realeaseFile.setInfoRealeaseId(bo.getInfoReleaseId());
        //        return  realeaseFile;
        //    }).collect(Collectors.toList());
        //    realeaseFileMapper.insertBatch(realeaseFiles);
        //}
        ossService.updateBiz(bo.getInfoReleaseId(), InfoRealease.class, "info_realease_header_pic",
            Collections.singletonList(bo.getInfoHeaderPicId()));
        ossService.updateBiz(bo.getInfoReleaseId(), InfoRealease.class, "info_realease_banner_id",
            Collections.singletonList(bo.getBannerId()));

        return flag;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(InfoRealease entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除信息发布信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return realeaseMapper.deleteByIds(ids) > 0;
    }

    @Override
    public List<InfoRealeaseVo> queryAppBannerList() {
            List<InfoRealeaseVo> list = realeaseMapper.queryAppBannerList();
            return list;
    }

    @Override
    public TableDataInfo<InfoRealeaseVo>  queryAppNoticeList(InfoRealeaseBo bo, PageQuery pageQuery) {
        Long userId = LoginHelper.getUserId();
        bo.setUserId(userId);
        Page<InfoRealeaseVo> result = realeaseMapper.queryAppNoticeList(pageQuery.build(), bo);
        return TableDataInfo.build(result);
    }
}
